package com.blb.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.entity.User;
import com.blb.service.UserService;
import com.blb.utils.LayuiPage;
import com.blb.utils.UUIDUtils;

@Controller
@RequestMapping("user")
public class UserController {

	private UserService userService = new UserService();
	
	@ResponseBody
	@RequestMapping("list")
	public LayuiPage page(int page, int limit, User user) throws SQLException, IOException{
		return userService.page((page-1)*limit, limit, user.getUsername(), user.getJob());
	}
	
	@ResponseBody
	@RequestMapping("add")
	public String add(User user){
		try {
			user.setId(UUIDUtils.getID());
			userService.addUser(user);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
			return "添加失败" + e.getMessage();
		}
		return "";
	}
	
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String username){
		try {
			User user = userService.getUserByUserName(username);
			if (user!=null) {
				return "用户名已存在";
			}else {
				return "";
			}
		} catch (SQLException | IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody String[] ids){
		try {
			userService.delete(ids);
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
}
